package l.l.w.leetcode.LetterCombinationsOfPhoneNumber;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by llw on 2017/10/28.
 */
public class LetterCombinationsOfPhoneNumber {

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() ==0) return Collections.emptyList();

        LinkedList<String> result = new LinkedList<>();
        HashMap<Character, String> map = new HashMap<>();
        map.put('1',"");
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");
        map.put('0',"");
        char[] one = new char[digits.length()];
        getResult(0,digits,result,one,map);
        return result;
    }

    private void getResult(int i, String digits, LinkedList<String> result, char[] one, HashMap<Character, String> map) {
        if(digits.length() == 0){
            result.add(new String(one));
            return;
        }
        String str = map.get(digits.charAt(0));
        for(int j = 0; j < str.length(); ++j){
            one[i]=str.charAt(j);
            getResult(i+1,digits.substring(1),result,one,map);
        }
    }

    public static void main(String[] args) {
        String a = "23";
        List<String> result = new LetterCombinationsOfPhoneNumber().letterCombinations(a);
        System.out.println(result);
    }
}
